package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.utils.security.PermissionUtils;
import com.ruoyi.system.domain.StuCertificate;
import com.ruoyi.system.domain.StuDataCertificate;
import com.ruoyi.system.service.IStuCertificateService;
import com.ruoyi.system.service.IStuDataCerService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.StuData;
import com.ruoyi.system.service.IStuDataService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生信息Controller
 * 
 * @author ruoyi
 * @date 2024-03-01
 */
@Controller
@RequestMapping("/system/studentData")
public class StuDataController extends BaseController
{
    private String prefix = "system/studentData";

    private Long nowUserId;
    private Long deptId;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IStuDataService stuDataService;
    @Autowired
    private IStuCertificateService stuCertificateService;
    @Autowired
    private IStuDataCerService stuDataCerService;

    @RequiresPermissions("system:studentData:view")
    @GetMapping()
    public String studentData()
    {
        return prefix + "/studentData";
    }

    /**
     * 查询学生信息列表
     */
    @RequiresPermissions("system:studentData:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StuData stuData)
    {
        startPage();
        nowUserId = (Long) PermissionUtils.getPrincipalProperty("userId");
        deptId = (Long) PermissionUtils.getPrincipalProperty("deptId");
        List<StuData> list = stuDataService.selectStuDataList(stuData);
        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @RequiresPermissions("system:studentData:export")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StuData stuData)
    {
        List<StuData> list = stuDataService.selectStuDataList(stuData);
        ExcelUtil<StuData> util = new ExcelUtil<StuData>(StuData.class);
        return util.exportExcel(list, "学生信息数据");
    }

    /**
     * 新增学生信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存学生信息
     */
    @RequiresPermissions("system:studentData:add")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StuData stuData)
    {
        stuData.setUserId(nowUserId);
        stuData.setDeptId(deptId);
        return toAjax(stuDataService.insertStuData(stuData));
    }

    /**
     * 修改学生信息
     */
    @RequiresPermissions("system:studentData:edit")
    @GetMapping("/edit/{stuId}")
    public String edit(@PathVariable("stuId") Long stuId, ModelMap mmap)
    {
        mmap.put("stuData", stuDataService.selectStuDataByStuId(stuId));
        return prefix + "/edit";
    }

    /**
     * 修改保存学生信息
     */
    @RequiresPermissions("system:studentData:edit")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StuData stuData)
    {
        return toAjax(stuDataService.updateStuData(stuData));
    }

    /**
     * 删除学生信息
     */
    @RequiresPermissions("system:studentData:remove")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        //stuDataCerService.deleteStuDataCerByStuIds(ids);
        return toAjax(stuDataService.deleteStuDataByStuIds(ids));
    }


    /**
     * 分配用户
     */
    @RequiresPermissions("system:studentData:auth")
    @GetMapping("/authCertificate/{stuId}")
    public String authCertificate(@PathVariable("stuId") Long stuId, ModelMap mmap) {
        mmap.put("stuId", stuId);
        return prefix + "/authCertificate";
    }

    /**
     * 查询已分配用户角色列表
     */
    @PostMapping("/authCertificate/allocatedList")
    @ResponseBody
    public TableDataInfo allocatedList(StuCertificate stuCertificate) {
        startPage();
        List<StuCertificate> list = stuCertificateService.selectAllocatedList(stuCertificate);
        return getDataTable(list);
    }

    /**
     * 取消授权
     */
    @RequiresPermissions("system:studentData:edit")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PostMapping("/authCertificate/cancel")
    @ResponseBody
    public AjaxResult cancelAuthCertificate(StuDataCertificate stuDataCertificate) {
        return toAjax(stuDataService.deleteAuthCertificate(stuDataCertificate));
    }

    /**
     * 批量取消授权
     */
    @RequiresPermissions("system:studentData:edit")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PostMapping("/authCertificate/cancelAll")
    @ResponseBody
    public AjaxResult cancelAuthCertificateAll(Long stuId, String cerIds) {
        return toAjax(stuDataService.deleteAuthCertificates(stuId, cerIds));
    }

    /**
     * 选择用户
     */
    @GetMapping("/authCertificate/selectCertificate/{stuId}")
    public String selectCertificate(@PathVariable("stuId") Long stuId, ModelMap mmap) {
        StuData stuData = new StuData();
        stuData.setStuId(stuId);
        mmap.put("stuData", stuData);
        return prefix + "/selectCertificate";
    }

    /**
     * 查询未分配用户角色列表
     */
    @PostMapping("/authCertificate/unallocatedList")
    @ResponseBody
    public TableDataInfo unallocatedList(StuCertificate stuCertificate) {
        startPage();
        List<StuCertificate> list = stuCertificateService.selectUnallocatedList(stuCertificate);
        return getDataTable(list);
    }

    /**
     * 批量选择用户授权
     */
    @RequiresPermissions("system:studentData:edit")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PostMapping("/authCertificate/selectAll")
    @ResponseBody
    public AjaxResult selectAuthCertificateAll(Long stuId, String cerIds) {
        return toAjax(stuDataService.insertAuthCertificates(stuId, cerIds));
    }
}
